/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package queenb.web.util;

import java.io.IOException;
import java.net.URL;
import java.util.Properties;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import org.apache.log4j.Logger;

/**
 *
 * @author thanhct
 */
public class ResourcesProvider {

    private static Logger logger = Logger.getLogger(ResourcesProvider.class);
    public static final String UPLOAD_MAIL= "resources/mail/";
    public static final String UPLOAD_FOLDER = "resources/kapi/";
    public static final String UPLOAD_FOLDER_TOUR = "resources/tour/";
    public static final String REPORT_EXCEL = "resources/excel/";


    public static String getRootPath() {
        URL url = ResourcesProvider.class.getResource("ResourcesProvider.class");
        logger.fatal("New Upload URL: " + url);
        logger.fatal("New Upload path: " + url.getPath());
        int indexOfWork = indexOfDeloyFolder("work", url);
        int indexOfWebApps = indexOfDeloyFolder("webapps", url);
        int indexOfWebINF = indexOfDeloyFolder("WEB-INF", url);
        String reVal = null;
        if (indexOfWork > 0) {
            //this case for deloy on tomcat
            reVal = url.getPath().substring(1, indexOfWork) + "webapps/";
            return reVal;
        } else if (indexOfWebApps > 0) {
            //this case for deloy on tomcat and work at tomcat's work folder
            reVal = url.getPath().substring(1, indexOfWebApps) + "webapps/";
            return reVal;
        } else {
            //this case for deloy on netbeans - may be have another case
            //if have any exception please check deloy folder to add on or contact me: thanhct
            reVal = url.getPath().substring(1, indexOfWebINF);
        }
        return reVal;
    }
    public static String getReportExcel() {
        logger.fatal("Upload Folder path: ------ || ------");
        String rootPath = getRootPath().replaceAll("%20", " ");
        logger.fatal("Folder to upload files: ||||=_=_=_=>>>>> " + rootPath + REPORT_EXCEL);
        return rootPath + REPORT_EXCEL;
    }
    private static int indexOfDeloyFolder(String folder, URL url) {
        return url.getPath().indexOf(folder);
    }

    public static String getUploadFolder() {
        logger.fatal("Upload Folder path: ------ || ------");
        String rootPath = getRootPath().replaceAll("%20", " ");
        logger.fatal("Folder to upload files: ||||=_=_=_=>>>>> " + rootPath + UPLOAD_FOLDER);
        //return rootPath + UPLOAD_FOLDER;
       return rootPath  + UPLOAD_FOLDER;
    }

    public static String getUploadMail() {
        logger.fatal("Upload Folder path: ------ || ------");
        String rootPath = getRootPath().replaceAll("%20", " ");
        logger.fatal("Folder to upload files: ||||=_=_=_=>>>>> " + rootPath + UPLOAD_MAIL);
        //return rootPath + UPLOAD_FOLDER;
       return rootPath  + UPLOAD_FOLDER;
    }

    
     public static String getUploadFolderTour() {
        logger.fatal("Upload Folder path: ------ || ------");
        String rootPath = getRootPath().replaceAll("%20", " ");
        logger.fatal("Folder to upload files: ||||=_=_=_=>>>>> " + rootPath + UPLOAD_FOLDER_TOUR);
        //return rootPath + UPLOAD_FOLDER;
       return rootPath  + UPLOAD_FOLDER_TOUR;
    }

    public static String getDownloadFolder(String fileName) {
       Object request = FacesContext.getCurrentInstance().getExternalContext().getRequest();
     
       String currentUrl = ((HttpServletRequest) request).getRequestURL().toString();
       
       if(currentUrl != null && !currentUrl.isEmpty() && currentUrl.contains("queenb")){
           String[] baseURL = currentUrl.split("kapi/");
        
           return baseURL[0]+ UPLOAD_FOLDER + fileName;
       }
       else{
            return "";
       }
    }
    
    public static String getDownloadFolderTour(String fileName) {
       Object request = FacesContext.getCurrentInstance().getExternalContext().getRequest();
     
       String currentUrl = ((HttpServletRequest) request).getRequestURL().toString();
       
       if(currentUrl != null && !currentUrl.isEmpty() && currentUrl.contains("queenb")){
           String[] baseURL = currentUrl.split("tour/");
        
           return baseURL[0]+ UPLOAD_FOLDER_TOUR + fileName;
       }
       else{
            return "";
       }
    }

    public static String getResources(String bundleKey) {
        String reVal = "";
        try {
            String resourcesValue = getProperty().getProperty(bundleKey);
            if (resourcesValue == null) {
                reVal = bundleKey + Resources.BUNDLE_KEY_NOT_FOUND;
            } else {
                reVal = resourcesValue;
            }
        } catch (IOException ex) {
            logger.debug("cann't get resources with key: " + bundleKey + " details:" + ex.getMessage());
            reVal = Resources.BUNDLE_FILE_NOT_FOUND;
        }
        return reVal;
    }

    public static Properties getProperty() throws IOException {
        Properties properties = new Properties();
        properties.load(ResourcesProvider.class.getResourceAsStream(Resources.RESOURCES_FILE_PATH));
        return properties;
    }
   

    public static void main(String arg[]) throws IOException {

        logger.fatal("Upload Folder path: ------ || ------");
        String rootPath = "c:\\program%20file\\tomcat%205.0\\".replaceAll("%20", " ");
        logger.fatal("Folder to upload files: ||||=_=_=_=>>>>> " + rootPath + UPLOAD_FOLDER);
        System.out.println(rootPath);
        System.out.println(getResources("mail.pop3.ssl"));
    }
}
